package com.molyfun.parents.fragment.dialog;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.utils.Const;

public class CommonChooseDialogFragment extends DialogFragment implements View.OnClickListener{

    private static final String FLAG_CANCEL_TEXT = "cancel";

    private static final String FLAG_CONFIRM_TEXT = "confirm";

    private static final String FLAG_CONFIRM_TITLE = "dialog_title";

    private static CommonChooseDialogFragment newInstance(String content) {
        CommonChooseDialogFragment mStudentMenuDialogFragment = new CommonChooseDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Const.FLAG_CONTENT, content);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }

    private static CommonChooseDialogFragment newInstance(String cancelBtn, String confirmBtn, String content, String title) {
        CommonChooseDialogFragment mStudentMenuDialogFragment = new CommonChooseDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Const.FLAG_CONTENT, content);
        bundle.putSerializable(FLAG_CANCEL_TEXT, cancelBtn);
        bundle.putSerializable(FLAG_CONFIRM_TEXT, confirmBtn);
        bundle.putSerializable(FLAG_CONFIRM_TITLE, title);
        mStudentMenuDialogFragment.setArguments(bundle);
        return mStudentMenuDialogFragment;
    }
//    @Override public void onStart() {
//        super.onStart();
//        Dialog dialog = getDialog();
//        if (null != dialog) {
//            dialog.getWindow().setLayout(-1, -2);
//        }
//    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setCancelable(false);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.BaseDialog);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        Window window = getDialog().getWindow();
//        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.gravity = Gravity.CENTER;
//        lp.width = getContext().getResources().getDimensionPixelOffset(R.dimen.dialog_common_width);//WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
//        window.setAttributes(lp);

        View view = inflater.inflate(R.layout.dialog_common_choose, container, false);
        String content = (String)getArguments().get(Const.FLAG_CONTENT);

        ((TextView)view.findViewById(R.id.content)).setText(content);
        if(!TextUtils.isEmpty(getArguments().getString(FLAG_CANCEL_TEXT))){
            ((TextView)view.findViewById(R.id.cancel)).setText(getArguments().getString(FLAG_CANCEL_TEXT));
        }
        if(!TextUtils.isEmpty(getArguments().getString(FLAG_CONFIRM_TEXT))){
            ((TextView)view.findViewById(R.id.commit)).setText(getArguments().getString(FLAG_CONFIRM_TEXT));
        }
        if(!TextUtils.isEmpty(getArguments().getString(FLAG_CONFIRM_TITLE))){
            ((TextView)view.findViewById(R.id.title)).setText(getArguments().getString(FLAG_CONFIRM_TITLE));
        }
        view.findViewById(R.id.cancel).setOnClickListener(this);
        view.findViewById(R.id.commit).setOnClickListener(this);
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.cancel:
                if(mCommonChooseDialogClickListener != null){
                    mCommonChooseDialogClickListener.onCancel();
                }
                break;
            case R.id.commit:
                if(mCommonChooseDialogClickListener != null){
                    mCommonChooseDialogClickListener.onConfirm();
                }
                break;
            default:
                if(mCommonChooseDialogClickListener != null){
                    mCommonChooseDialogClickListener.onCancel();
                }
                break;
        }
        getDialog().dismiss();
    }

    public interface CommonChooseDialogClickListener {
        public void onConfirm();
        public void onCancel();
    }

    private CommonChooseDialogClickListener mCommonChooseDialogClickListener;

    public void setCommonChooseDialogClickListener(CommonChooseDialogClickListener commonChooseDialogClickListener){
        this.mCommonChooseDialogClickListener = commonChooseDialogClickListener;
    }


    public static class CommonChooseDialogBuilder {

        CommonChooseDialogFragment mCommonDialogFragment;
        private String mTag;
        private CommonChooseDialogClickListener mCommonChooseDialogClickListener;

        public CommonChooseDialogBuilder(String content){
            mCommonDialogFragment = CommonChooseDialogFragment.newInstance(content);
            mTag = "common_choose_notice";
        }

        public CommonChooseDialogBuilder(String cancelBtn, String confirmBtn, String content){
            mCommonDialogFragment = CommonChooseDialogFragment.newInstance(cancelBtn, confirmBtn, content, "提示");
            mTag = "common_choose_notice";
        }

        public CommonChooseDialogBuilder(String cancelBtn, String confirmBtn, String content, String title){
            mCommonDialogFragment = CommonChooseDialogFragment.newInstance(cancelBtn, confirmBtn, content, title);
            mTag = "common_choose_notice";
        }

        public CommonChooseDialogBuilder setCommonChooseDialogClickListener(CommonChooseDialogClickListener commonChooseDialogClickListener){
            mCommonDialogFragment.setCommonChooseDialogClickListener(commonChooseDialogClickListener);
            return this;
        }


        public CommonChooseDialogBuilder setTag(String tag){
            this.mTag = tag;
            return this;
        }

        public void build(FragmentTransaction fragmentTransaction){
            fragmentTransaction.add(mCommonDialogFragment, mTag);
            fragmentTransaction.commitAllowingStateLoss();
//            mCommonDialogFragment.show(fragmentTransaction, mTag);
        }

    }
}
